package com.besttone.search;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.besttone.adapter.AreaAdapter;
import com.besttone.adapter.CateAdapter;
import com.besttone.adapter.SortAdapter;
import com.besttone.http.WebServiceHelper;
import com.besttone.search.model.District;
import com.besttone.search.model.RequestInfo;
import com.besttone.search.util.Constants;
import com.besttone.search.util.SharedUtils;
import com.besttone.widget.PoiListItem;
import com.besttone.search.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.CommonDataKinds.StructuredName;
import android.provider.ContactsContract.CommonDataKinds.StructuredPostal;
import android.provider.ContactsContract.Contacts;
import android.provider.ContactsContract.Data;
import android.provider.ContactsContract.RawContacts;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.Toast;

public class SearchResultActivity extends Activity implements ServerListener,
		OnClickListener {
	private Context mContext;
	private List<Map<String, Object>> filterData;
	private View loadingView;
	private View addShopView;
	private ListView list;
	private boolean isEnd = false;
	PoiResultAdapter resultAdapter = null;
	ListAdapter areaAdapter = null;
	CateAdapter cateAdapter = null;
	private ImageButton btn_back;
	private Button mSearchBtn;
	private Button mAreaBtn;
	private String[] mAreaArray = null;
	private List<District> localDistrictList = null;
	private String[] mChannelArray = null;
	private int mAreaIndex = 0;
	private Button mChannelBtn;
	public PopupWindow mPopupWindow;
	private ListView popListView;
	private String keyword;
	
	private RequestInfo rqInfo;
	private WebServiceHelper serviceHelper;

	private boolean isLoadingRemoved = false;
	private boolean isLoadingEnd = false;

	Handler handler = new Handler() {
		public void handleMessage(Message paramMessage) {
			if (paramMessage.what == 1) {
				loadingView.setVisibility(View.GONE);
			} else if (paramMessage.what == 2) {
				list.removeFooterView(loadingView);
				isLoadingRemoved = true;
			} else if (paramMessage.what == 3) {
				list.addFooterView(loadingView);
				loadingView.setVisibility(View.VISIBLE);
				isLoadingRemoved = false;
			} else if (paramMessage.what == 4) {
				loadingView.setVisibility(View.VISIBLE);
			} else  if (paramMessage.what == 5) {
				list.removeFooterView(loadingView);
				isLoadingRemoved = true;
				if(!isLoadingEnd)
					addShopView();
			}
		}
	};

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.search_result);
		
		mContext = this.getApplicationContext();

		init();
	}
	
	private ImageButton.OnClickListener backListner = new ImageButton.OnClickListener() {
		public void onClick(View v) {
			finish();
		}
	};
	
    private Button.OnClickListener searchHistroyListner = new Button.OnClickListener() {
    	public void onClick(View v) {
			Intent intent = new Intent();
			intent.setClass(SearchResultActivity.this, SearchActivity.class);
			finish();
			startActivity(intent);
    	}
    };

	private AdapterView.OnItemClickListener mOnClickListener = new AdapterView.OnItemClickListener() {
		public void onItemClick(AdapterView<?> parent, View v, int position,
				long id) {
//			 Intent intent = new Intent();
//			 ListView listView = (ListView)parent;
//			 HashMap<String, String> map = (HashMap<String, String>) listView.getItemAtPosition(position);
//			 intent.setClass(ResultActivity.this, DetailActivity.class);
//			 if(map.get("id")==null || "".equals(map.get("id"))) {
//				 intent.putExtra("id", "2029602847-421506714");
//			 } else {
//				 intent.putExtra("id", map.get("id"));
//			 }
//			 startActivity(intent);
			// ResultActivity.this.finish();
			//显示数据详情
			//Toast.makeText(SearchResultActivity.this, "正在开发中，敬请期待...", Toast.LENGTH_SHORT).show();
		}
	};

	public class PoiResultAdapter extends BaseAdapter {
		private LayoutInflater mInflater;

		public PoiResultAdapter(Context context) {
			this.mInflater = LayoutInflater.from(context);
		}

		public int getCount() {
			return filterData.size();
		}

		public Object getItem(int position) {
			return filterData.get(position);
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {

			if (convertView == null) {
				
			}

			convertView = mInflater.inflate(R.layout.search_result_item, null);
//			convertView.findViewById(R.id.name).setSelected(true);
//			convertView.findViewById(R.id.addr).setSelected(true);

			PoiListItem item = (PoiListItem) convertView;

			Map map = filterData.get(position);

			item.setPoiData(map.get("name").toString(), map.get("tel")
					.toString(), map.get("addr").toString(),(map
							.get("city")==null?null:(map
									.get("city")).toString()) );

			if (position == filterData.size() - 1 && !isEnd) {
				loadingView.setVisibility(View.VISIBLE);
				//下拉获取数据
				int size = filterData.size();
				int page = 1 + size/Constants.PAGE_SIZE;
				int currentPage =Integer.valueOf(rqInfo.getPage());
				if(size==currentPage*Constants.PAGE_SIZE) {
					rqInfo.setPage(Constants.SPACE + page);
					serviceHelper.sendRequest(SearchResultActivity.this, rqInfo);
				} else {
					//Toast.makeText(ResultActivity.this, "没有更多的数据", Toast.LENGTH_SHORT).show();
					Message localMessage = new Message();
					localMessage.what = 5;
					handler.sendMessage(localMessage);
				}
				
			}
			return convertView;
		}

	}
	
	public void addShopView() {
		isLoadingEnd = true;
		addShopView = getLayoutInflater().inflate(R.layout.search_result_empty, null, false);
		list.addFooterView(addShopView);
		addShopView.setVisibility(View.VISIBLE);
	}

	public void serverDataArrived(List list, boolean isEnd) {
		this.isEnd = isEnd;
		Iterator iter = list.iterator();
		while (iter.hasNext()) {
			filterData.add((Map<String, Object>) iter.next());
		}
		Message localMessage = new Message();
		if (!isEnd) {
			localMessage.what = 1;
		} else {
			localMessage.what = 2;
		}
		if(list!=null && list.size()==0){
			localMessage.what = 5;
		}

		this.handler.sendMessage(localMessage);
	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.area: {
			showDialogPopup(R.id.area);

			break;
		}
		case R.id.channel: {
			showDialogPopup(R.id.channel);
//			if  (mPopupWindow !=  null && mPopupWindow.isShowing()) {
//			mPopupWindow.dismiss();
//        } else {
//        	createPopWindow(v);
//        }
			break;
		}
		}

	}

	protected void showDialogPopup(int viewId) {
		AlertDialog.Builder localBuilder = new AlertDialog.Builder(this);

		switch (viewId) {
		case R.id.area: {

			if (areaAdapter == null) {
				areaAdapter = new AreaAdapter(this, mAreaArray);
			}

			localBuilder.setAdapter(areaAdapter, new areaPopupListener(
					areaAdapter));

			break;
		}

		case R.id.channel: {
			if (cateAdapter == null) {
				cateAdapter = new CateAdapter(this, mChannelArray);
			}
			localBuilder.setAdapter(cateAdapter, new channelPopupListener(
					cateAdapter));
			break;
		}

		}

		AlertDialog localAlertDialog = localBuilder.create();
		localAlertDialog.show();
	}

	class areaPopupListener implements DialogInterface.OnClickListener {
		AreaAdapter mAdapter;

		public areaPopupListener(ListAdapter adapter) {
			mAdapter = (AreaAdapter) adapter;
		}

		public void onClick(DialogInterface dialog, int which) {
				((AreaAdapter) mAdapter).setTypeIndex(which);
				final String cityName = ((AreaAdapter) mAdapter).getSelect();
				mAreaBtn.setText(cityName);

				filterData.clear();
				
				filterData = new ArrayList<Map<String, Object>>();
				resultAdapter.notifyDataSetChanged();

				if (isLoadingRemoved) {
					list.addFooterView(loadingView);
					loadingView.setVisibility(View.VISIBLE);
					isLoadingRemoved = false;
				} else {
					loadingView.setVisibility(View.VISIBLE);
				}
				
				if(isLoadingEnd){
					list.removeFooterView(addShopView);
					isLoadingEnd = false;
				}

				//获取数据
				String simplifyCode = null;
				if("全部区域".equals(cityName)) {
					simplifyCode = SharedUtils.getCurrentSimplifyCode(mContext);
				} else {
					simplifyCode = ((District)(localDistrictList.get(which-1))).getDistrictCode();
//					simplifyCode = Constants.getSimplifyCodeByName(mContext, cityName);
				}
//				Log.d("getdis",simplifyCode);
				rqInfo.setRegion(simplifyCode);
				
				String tradeName = mChannelBtn.getText().toString();
				if(!"全部行业".equals(tradeName)) {
					rqInfo.setContent(keyword  + "/n|" + tradeName + "/i");
				} else {
					rqInfo.setContent(keyword+"/n");
				}
				
				rqInfo.setPage("1");
				rqInfo.setPageSize(Constants.SPACE + Constants.PAGE_SIZE);
				serviceHelper = new WebServiceHelper();
				serviceHelper.sendRequest(SearchResultActivity.this, rqInfo);
		}

	}
	
	class channelPopupListener implements DialogInterface.OnClickListener {
		CateAdapter cAdapter;
		
		public channelPopupListener(CateAdapter adapter) {
			cAdapter = (CateAdapter) adapter;
		}

		public void onClick(DialogInterface dialog, int which) {
			((CateAdapter) cAdapter).setTypeIndex(which);
			final String tradeName = ((CateAdapter) cAdapter).getSelect();
			mChannelBtn.setText(tradeName);
			
			filterData.clear();
			
			filterData = new ArrayList<Map<String, Object>>();
			resultAdapter.notifyDataSetChanged();
			
			if (isLoadingRemoved) {
				list.addFooterView(loadingView);
				loadingView.setVisibility(View.VISIBLE);
				isLoadingRemoved = false;
			} else {
				loadingView.setVisibility(View.VISIBLE);
			}
			
			if(isLoadingEnd){
				list.removeFooterView(addShopView);
				isLoadingEnd = false;
			}
			
			//获取数据
			String simplifyCode = null;
			String cityName = mAreaBtn.getText().toString();
			if("全部区域".equals(cityName)) {
				simplifyCode = SharedUtils.getCurrentSimplifyCode(mContext);
			} else {
				simplifyCode = Constants.getSimplifyCodeByName(mContext, cityName);
			}
			
			if(!"全部行业".equals(tradeName)) {
				rqInfo.setContent(keyword  + "/n|" + tradeName + "/i");
			} else {
				rqInfo.setContent(keyword+"/n");
			}
			
			rqInfo.setRegion(simplifyCode);
			rqInfo.setPage("1");
			rqInfo.setPageSize(Constants.SPACE + Constants.PAGE_SIZE);
			serviceHelper = new WebServiceHelper();
			serviceHelper.sendRequest(SearchResultActivity.this, rqInfo);
			
			
		}
		
	}
	
	  private void init()
	  {
		  filterData = PoiResultData.getData();

			list = (ListView) findViewById(R.id.resultlist);
			list.setFastScrollEnabled(true);
			resultAdapter = new PoiResultAdapter(this);
			
			btn_back = (ImageButton) findViewById(R.id.left_title_button);
			btn_back.setOnClickListener(backListner);
			
			rqInfo = new RequestInfo();
			Bundle bundle = this.getIntent().getExtras();
			rqInfo.setRegion(SharedUtils.getCurrentSimplifyCode(this));
			rqInfo.setDeviceid(SharedUtils.getCurrentPhoneDeviceId(this));
			rqInfo.setImsi(SharedUtils.getCurrentPhoneImsi(this));
			rqInfo.setMobile(SharedUtils.getCurrentPhoneNo(this));
			rqInfo.setPage("1");
			rqInfo.setPageSize(Constants.SPACE + Constants.PAGE_SIZE);
			keyword = bundle.getString("keyword");
			rqInfo.setContent(keyword+"/n");
			serviceHelper = new WebServiceHelper();
			serviceHelper.sendRequest(this, rqInfo);
			
		    mSearchBtn = ((Button)findViewById(R.id.start_search));
		    mSearchBtn.setText(bundle.getString("keyword"));
		    mSearchBtn.setOnClickListener(searchHistroyListner);
			
			

			loadingView = LayoutInflater.from(this).inflate(R.layout.listfooter,
					null);

			list.addFooterView(loadingView);

			list.setAdapter(resultAdapter);
			list.setOnItemClickListener(mOnClickListener);
			list.setOnItemLongClickListener(mOnLongClickListener);

	    mAreaBtn = ((Button)findViewById(R.id.area));
	    mChannelBtn = ((Button)findViewById(R.id.channel));
	    mAreaBtn.setOnClickListener(this);
	    mChannelBtn.setOnClickListener(this);
	    initHomeView();
	  }
	  
	private void initHomeView() {
		localDistrictList = Constants.getDistrictList(this, SharedUtils.getCurrentCityCode(this));
		mAreaArray = Constants.getDistrictArray(this, SharedUtils.getCurrentCityCode(this));
		if ((mAreaArray == null) || (this.mAreaArray.length == 0)) {
			mAreaBtn.setText("全部区域");
			mAreaBtn.setEnabled(false);
		}
		while (true) {
			mChannelArray = getResources().getStringArray(R.array.trade_name_items);
			mAreaBtn.setText(this.mAreaArray[this.mAreaIndex]);
			mAreaBtn.setEnabled(true);
			return;
		}
	}
	
	private   void  createPopWindow(View parent) {
//			LayoutInflater lay = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//			View view = lay.inflate(R.layout.popup_window, null );
//			
//			mPopupWindow = new PopupWindow(view, LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
//		
//			//初始化listview，加载数据
//			popListView = (ListView) findViewById(R.id.pop_list);
////			if (sortAdapter == null) {
////				sortAdapter = new SortAdapter(this, mSortArray);
////			}
////			popListView.setAdapter(sortAdapter);	
//
//		
//		//设置整个popupwindow的样式
//		mPopupWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.right_filter_bg));
//		
//		mPopupWindow.setFocusable(true );
//		mPopupWindow.setTouchable(true);
//		mPopupWindow.setOutsideTouchable(true);
//		mPopupWindow.update();
//		mPopupWindow.showAsDropDown(parent, 10, 10);
		View contentView = LayoutInflater.from(getApplicationContext())
				.inflate(R.layout.popup_window, null);

		mPopupWindow = new PopupWindow(contentView, LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
		mPopupWindow.setContentView(contentView);

		//设置整个popupwindow的样式
		mPopupWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.right_filter_bg));
	
		
		String[] name = openDir();
		ListView listView = (ListView) contentView.findViewById(R.id.pop_list);
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, name);
		listView.setAdapter(adapter);
		mPopupWindow.setFocusable(true );
		mPopupWindow.setTouchable(true);
		mPopupWindow.setOutsideTouchable(true);
		mPopupWindow.update();
		mPopupWindow.showAsDropDown(parent, 10, 10);

	}

	private String[] openDir() {
		String[] name;
		String rootPath = Environment.getExternalStorageDirectory()
				.getAbsolutePath();
		File file = new File(rootPath);
		File[] files = file.listFiles();
		name = new String[files.length];
		for (int i = 0; i < files.length; i++) {
			name[i] = files[i].getName();
			System.out.println(name[i]);
		}
		return name;
	}
	
	private AdapterView.OnItemLongClickListener mOnLongClickListener = new AdapterView.OnItemLongClickListener() {

		public boolean onItemLongClick(AdapterView<?> parent, View view,
				int position, long id) {
			
			 ListView listView = (ListView)parent;
			 HashMap<String, String> map = (HashMap<String, String>) listView.getItemAtPosition(position);
			 final String name = map.get("name");
			 final String tel = map.get("tel");
			 final String addr = map.get("addr");
			 String[] array = new String[2];
			array[0] = "呼叫 " + tel;
			array[1] = "添加至联系人";
			
			new AlertDialog.Builder(SearchResultActivity.this)
					.setTitle(name)
					.setItems(array,
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int which) {
									if(which == 0) {
										Intent myIntentDial = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + tel));
										startActivity(myIntentDial);
									}
									if(which == 1){
										String rwContactId = getContactId(name);
										ContentResolver resolver = mContext.getContentResolver();
										ContentValues values = new ContentValues();
										//联系人中是否存在，若存在则更新
										if(rwContactId!=null && !"".equals(rwContactId)){
											Toast.makeText(SearchResultActivity.this, "商家已存在", Toast.LENGTH_SHORT).show();
//											String whereClause = ContactsContract.RawContacts.Data.RAW_CONTACT_ID + "= ? AND " + ContactsContract.Data.MIMETYPE + "=?";
//											
//					                        values.clear();
//					                        values.put(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE);
//					                        values.put(StructuredName.DISPLAY_NAME, name);
//					                        resolver.update(Data.CONTENT_URI, values, whereClause, new String[]{rwContactId, StructuredName.CONTENT_ITEM_TYPE });
//											
//					                        values.clear();
//					                        values.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
//					                        values.put(Phone.NUMBER, tel);
//					                        if(isMobilePhone(tel)){
//					                        	values.put(Phone.TYPE, Phone.TYPE_MOBILE);
//					                        	values.put(Phone.LABEL, Phone.TYPE_WORK_MOBILE);
//					                        } else {
//					                        	values.put(Phone.TYPE, Phone.TYPE_WORK);
//					                        	values.put(Phone.LABEL, Phone.TYPE_WORK);
//					                        }
//					                        resolver.update(Data.CONTENT_URI, values, whereClause, new String[]{rwContactId, Phone.CONTENT_ITEM_TYPE});
//					                        
//					                        values.clear();
//					                        values.put(Data.MIMETYPE, StructuredPostal.CONTENT_ITEM_TYPE);
//					                        values.put(StructuredPostal.TYPE, StructuredPostal.TYPE_WORK);
//					                        values.put(StructuredPostal.LABEL, StructuredPostal.TYPE_WORK);
//					                        values.put(StructuredPostal.STREET, addr);
//					                        resolver.update(Data.CONTENT_URI, values, whereClause,  new String[]{rwContactId, StructuredPostal.CONTENT_ITEM_TYPE});
//					                        
//					                        Toast.makeText(SearchResultActivity.this, "联系人更新成功", Toast.LENGTH_SHORT).show();
										} else {
					                        Uri rawContactUri = resolver.insert(
					                                        RawContacts.CONTENT_URI, values);
					                        long rawContactId = ContentUris.parseId(rawContactUri);
	
					                        values.clear();
					                        values.put(Data.RAW_CONTACT_ID, rawContactId);
					                        values.put(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE);
					                        values.put(StructuredName.DISPLAY_NAME, name);
					                        resolver.insert(Data.CONTENT_URI, values);
					                        
					                        values.clear();
					                        values.put(Data.RAW_CONTACT_ID, rawContactId);
					                        values.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
					                        values.put(Phone.NUMBER, tel);
					                        if(isMobilePhone(tel)){
					                        	values.put(Phone.TYPE, Phone.TYPE_MOBILE);
					                        	values.put(Phone.LABEL, Phone.TYPE_WORK_MOBILE);
					                        } else {
					                        	values.put(Phone.TYPE, Phone.TYPE_WORK);
					                        	values.put(Phone.LABEL, Phone.TYPE_WORK);
					                        }
					                        resolver.insert(Data.CONTENT_URI, values);
					                        
					                        values.clear();
					                        values.put(Data.RAW_CONTACT_ID, rawContactId);
					                        values.put(Data.MIMETYPE, StructuredPostal.CONTENT_ITEM_TYPE);
					                        values.put(StructuredPostal.TYPE, StructuredPostal.TYPE_WORK);
					                        values.put(StructuredPostal.LABEL, StructuredPostal.TYPE_WORK);
					                        values.put(StructuredPostal.STREET, addr);
					                        resolver.insert(Data.CONTENT_URI, values);
					                        Toast.makeText(SearchResultActivity.this, "添加成功", Toast.LENGTH_SHORT).show();
										}
										
										
									}

								}
							})
					.setNegativeButton("取消",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int which) {

								}
							}).show();

			return true;
		}

	};

	public boolean isMobilePhone(String tel){
		boolean flag = false;
		if(tel!=null && tel.length()==11){
			String reg = "^(13[0-9]|15[012356789]|18[0236789]|14[57])[0-9]{8}$";
			flag = tel.matches(reg);
		}
		return flag;
	}
	
	/*
	 * 根据电话号码取得联系人姓名
	 */
	public void getPeople() {
		String[] projection = { ContactsContract.PhoneLookup.DISPLAY_NAME,
								ContactsContract.CommonDataKinds.Phone.NUMBER};
 
		// 将自己添加到 msPeers 中
		Cursor cursor = this.getContentResolver().query(
				ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                projection,    // Which columns to return.
                ContactsContract.CommonDataKinds.Phone.NUMBER + " = '021-65291718'", // WHERE clause.
                null,          // WHERE clause value substitution
                null);   // Sort order.
 
		if( cursor == null ) {
		    return;
		}
		System.out.println(cursor.getCount());
		for( int i = 0; i < cursor.getCount(); i++ )
		{
			cursor.moveToPosition(i);
 
			// 取得联系人名字
            int nameFieldColumnIndex = cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME);   
            String name = cursor.getString(nameFieldColumnIndex);
	        System.out.println("联系人姓名：" + name);
	        Toast.makeText(SearchResultActivity.this, "联系人姓名：" + name, Toast.LENGTH_SHORT).show();
		}
	}
	
	/*
	 * 根据联系人姓名取得ID
	 */
	public String getContactId(String name) {
		String rawContactId = null;
		String[] projection = { ContactsContract.RawContacts.Data.RAW_CONTACT_ID };
		// 将自己添加到 msPeers 中
		Cursor cursor = this.getContentResolver().query(
				ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                projection,    // Which columns to return.
                ContactsContract.PhoneLookup.DISPLAY_NAME + " = '"+ name +"'", // WHERE clause.
                null,          // WHERE clause value substitution
                null);   // Sort order.
 
		if( cursor == null ) {
		    return null;
		} else if(cursor.getCount()>0){
				cursor.moveToFirst();
				// 取得联系人ID
	            int idFieldColumnIndex = cursor.getColumnIndex(ContactsContract.RawContacts.Data.RAW_CONTACT_ID);
				rawContactId = cursor.getString(idFieldColumnIndex);
				return rawContactId;
			}
		return null;
	}
}